const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    test: 1,
    defaultAvatarUrl: defaultAvatarUrl,
    login:false
  },

  goPage(e) {
    console.log(e);
    let page = e.currentTarget.dataset.page
    if(page=='bookRecord'){
      wx.showToast({
        title: '该功能正在路上哦',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '../' + page + '/' + page,
    })
  },

  GetuserinfoHide(res) {
    var that = this
    // console.log('用户信息',res.detail.userInfo);
    var userInfo = res.detail.userInfo
    wx.showModal({
      title: '快乐站台申请',
      content: '获取你的昵称、头像、地区及性别',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        }
        if (res.confirm) {
          wx.login({
            success: res => {
              if (res.code) {
                console.log(res.code);
                let URL = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxc3cce0152994d627&secret=c0f0108bd7593b04b31a097e49bbda90&js_code=' + res.code + '&grant_type=authorization_code'
                wx.request({
                  url: URL,
                  success: function (res) {
                    // console.log(res)
                    let openid = res.data.openid
                    that.login(userInfo, openid)
                    wx.setStorageSync('openid', openid)
                    app.globalData.openid = openid
                    that.setData({
                      openid: openid,
                    })
                  }
                })
              }
            }
          })
          app.globalData.userInfo = userInfo
          wx.setStorageSync('userInfo', userInfo)
          this.setData({
            userInfo: userInfo
          })
          wx.showToast({
            title: '登录成功',
            icon: 'none'
          })
        }
      }
    })
  },

  // 登陆方法
  login(userInfo, openid) {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'nine_thirth',
      config: {
        env: app.globalData.env
      },
      data: {
        cloud_function: "login",
        openid: openid,
        userInfo: userInfo,
      }
    }).then((resp) => {
      console.log("信息：", resp)
      let data = resp.result.data
      if (data.status == 200) {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
        let userInfo = data.userInfo.userInfo
        wx.setStorageSync('userInfo',userInfo)
        this.setData({
          userInfo: data.userInfo.userInfo,
          openid: data.openid,
          login: true
        })
      } else {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
        this.setData({
          login: true
        })
      }
      wx.hideLoading()
    }).catch((e) => {
      console.error('获取数据失败:', e);
      wx.hideLoading()

    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getUserInfo(){
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'nine_thirth',
      config: {
        env: app.globalData.env
      },
      data: {
        cloud_function: "getUserInfo",
        openid: wx.getStorageSync('openid'),
      }
    }).then((resp) => {
      console.log("信息：", resp)
      let data = resp.result.data
      if (data.status == 200) {
        // wx.showToast({
        //   title: data.message,
        //   icon: 'none'
        // })
        this.setData({
          userInfo: data.userInfo.userInfo,
        })
      } else {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
      }
      wx.hideLoading()
    }).catch((e) => {
      console.error('获取数据失败:', e);
      wx.hideLoading()

    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("jjj");
    let userInfo = wx.getStorageSync('userInfo')

    if (app.isLogin()) {
      this.getUserInfo()
      this.setData({
        login: true
      })
    } else if (userInfo && userInfo !== "") {
      this.setData({
        userInfo: userInfo,
        login: true
      })
    } else {
      this.setData({
        login: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})